package com.heima.media.controller;

import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.dto.WmMaterialDto;
import com.heima.media.entity.WmMaterial;
import com.heima.media.service.IWmMaterialService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 自媒体图文素材信息表 前端控制器
 * </p>
 *
 * @author Wq
 * @since 2022-10-09
 */
@RestController
@RequestMapping("/api/v1/material")
@Api(tags = "自媒体图文素材信息表接口")
@CrossOrigin
public class WmMaterialController {

    @Autowired
    private IWmMaterialService wmMaterialService;

    /**
     * 上传照片
     *
     * @param file 文件
     * @return {@code ResponseResult}
     */
    @PostMapping("upload_picture")
    public ResponseResult<WmMaterial> uploadPicture(MultipartFile file) {
        return wmMaterialService.uploadPicture(file);
    }

    /**
     * 展示素材列表
     *
     * @return {@code ResponseResult}
     */
    @PostMapping("list")
    public ResponseResult showMaterialList(@RequestBody WmMaterialDto dto) {
        return wmMaterialService.showMaterialList(dto);
    }


    /**
     * 根据id删除素材
     *
     * @param id id
     * @return {@code ResponseResult}
     */
    @DeleteMapping("/{id}")
    public ResponseResult deleteById(@PathVariable("id") Integer id) {

        wmMaterialService.removeById(id);
        return ResponseResult.okResult();
    }

    /**
     * 自媒体素材收藏
     *
     * @return {@link ResponseResult}
     */
    @PutMapping("/collect/{id}")
    public ResponseResult collect(@PathVariable("id") Integer id) {
        WmMaterial wmMaterial = wmMaterialService.getById(id);
        if (wmMaterial == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        wmMaterial.setIsCollection(true);
        wmMaterialService.updateById(wmMaterial);
        return ResponseResult.okResult();
    }

    // TODO: 2022/10/17 收藏和取消收藏可以二合一
    /**
     * 自媒体素材取消收藏
     *
     * @param id id
     * @return {@link ResponseResult}
     */
    @PutMapping("/cancel_collect/{id}")
    public ResponseResult cancelCollect(@PathVariable("id") Integer id) {
        WmMaterial wmMaterial = wmMaterialService.getById(id);
        if (wmMaterial == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        wmMaterial.setIsCollection(false);
        wmMaterialService.updateById(wmMaterial);
        return ResponseResult.okResult();
    }


}
